package com.xjgc.common.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.io.IOException;

/**
 * 字符串转成Double类型
 * Created by ThinkPad on 2019/12/9.
 */
public class StContverDouSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if(StringUtils.isNotBlank(value)) {
            Double toDouble=NumberUtils.toDouble(value);
            gen.writeNumber(toDouble);
        }else {//这个分支不要忘记了，否则将不输出这个属性的值
            gen.writeNumber(0.0);
        }
    }
}
